/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-09-06
 * Time:10:49
 */
//String 类

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("nnnnn");

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("hhhhh");
    }
    public static void main23(String[] args) {
        //String转StringBuilder
        String s = "hello";
        StringBuilder stringBuilder = new StringBuilder(s);
        //或者
        stringBuilder.append(s);

        //StringBuilder转String
        StringBuilder stringBuilder1 = new StringBuilder("world");
        String s1 = stringBuilder1.toString();
    }
    public static void main22(String[] args) {
        //反转字符串
        StringBuilder stringBuilder = new StringBuilder("abcdef");
        stringBuilder.reverse();
        System.out.println(stringBuilder);
        //字符串截取
        StringBuilder stringBuilder1 = new StringBuilder("hhaldefrs");
        String ret = stringBuilder1.substring(4);
        System.out.println(ret);
        //字符串插入
        StringBuilder stringBuilder2 = new StringBuilder("ababcabcd");
        stringBuilder2.insert(3,"hhh");
        System.out.println(stringBuilder2);
    }
    public static void main21(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        //当执行+=操作时，Java实际上会创建一个StringBuilder对象
        //使用StringBuilder的append()方法拼接字符串
        stringBuilder.append(" world");
        //调用toString()方法生成新的String对象
        String ret = stringBuilder.toString();
        System.out.println(ret);
        //将新对象的引用赋给变量s: s = ret;
    }
    public static void main20(String[] args) {
        String s = "hello";
        s += " world";
        System.out.println(s);//输出 hello world

        String ss = "123";
        String ss1 = "12345";
        ss = ss1;
        System.out.println(ss);
    }
    public static void main19(String[] args) {
        final char[] array = {'a','b','c'};
        array[0] = 'g';//引用的空间中的内容可以修改

        //array = new char[10];//不能引用其他字符数组

        final String s = "abc";
        char ch = s.charAt(0);
        ch = 'n';
        System.out.println(ch);
    }
    public static void main18(String[] args) {
        //trim方法
        String s = "  ab  c d e f   ";
        System.out.println("去掉开头和结尾处空白字符前："+s);
        String s1 = s.trim();
        System.out.println("去掉开头和结尾处空白字符后："+s1);
    }
    public static void main17(String[] args) {
        //字符串截取
        String s = "abcdef";
        String s2 = s.substring(2);
        System.out.println(s2);
        String s3 = s.substring(2,4);//[2,4)
        System.out.println(s3);
    }
    public static void main16(String[] args) {
        String s = "name=zhangsan&age=10";
        String[] strings = s.split("&|=");//顺序可换
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }
    public static void main15(String[] args) {
        String s = "a\\b\\c";
        System.out.println(s);
        String[] strings = s.split("\\\\");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }
    public static void main14(String[] args) {
        String s = "192.168.1.1";
        String[] strings = s.split("\\.");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }
    }
    public static void main13(String[] args) {
        //字符串拆分
        String s = "name=zhangsan&age=10";
        String[] strings = s.split("&");
        for (int i = 0; i < strings.length; i++) {
            String[] ss = strings[i].split("=");
            for (int j = 0; j < ss.length; j++) {
                System.out.print(ss[j]+" ");
            }
            System.out.println();
        }
    }
    public static void main12(String[] args) {
        //字符串替换
        String s = "ababcabcd";
        String tmp = s.replace('a','k');
        System.out.println(tmp);
        String tmp1 = s.replace("ab","uuu");
        System.out.println(tmp1);
        String tmp2 = s.replaceFirst("ab","o");
        System.out.println(tmp2);
        String tmp3 = s.replaceAll("ab","o");
        System.out.println(tmp3);
    }
    public static void main11(String[] args) {
        //格式化输出
        String s = String.format("%d-%d-%d",2025,9,6);
        System.out.println(s);
    }
    public static void main10(String[] args) {
        //字符串转数组
        String s = "world";
        char[] ch = s.toCharArray();

        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i] + " ");
        }
        System.out.println();
        System.out.println("===========");
        //数组转字符串
        char[] ch1 = {'w','o','r','l','d'};
        //String s1 = new String(ch1);
        String s1 = new String(ch);
        System.out.println(s1);
    }
    public static void main9(String[] args) {
        //大小写转换
        String s1 = "hello";
        String ss1 = s1.toUpperCase();
        String s2 = "HELLO";
        String ss2 = s2.toLowerCase();
        System.out.println(ss1);
        System.out.println(ss2);
    }
    public static void main8(String[] args) {
        //数字转字符串
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        //将对象转化为字符串
        String s4 = String.valueOf(new Student("zhangsan",10));
        System.out.println(s4);
        //字符串转数字
        int date1 = Integer.parseInt("1234");
        double date2 = Double.parseDouble("12.5");
        System.out.println(date1 + 1);
        System.out.println(date2);
    }
    public static void main7(String[] args) {
        String s = "hello";
        //从前往后找,默认从0位置开始
        System.out.println(s.indexOf('l'));//返回字符'l'第一次出现的位置，即返回的是下标
        System.out.println(s.indexOf('l', 3));//从下标为3的位置开始找字符'l'第一次出现的位置
        System.out.println(s.indexOf("lo"));//返回字符串"lo"第一次出现的位置
        System.out.println(s.indexOf("lo", 1));//从下标为1的位置开始找字符串"lo"第一次出现的位置
        System.out.println("========");
        //从后往前找
        System.out.println(s.lastIndexOf('l'));//从后往前找字符'l'第一次出现的位置
        System.out.println(s.lastIndexOf('l', 2));//从下标为2的位置开始从后往前找字符'l'第一次出现的位置
        System.out.println(s.lastIndexOf("ell"));//从后往前找字符串"llo"第一次出现的位置
        System.out.println(s.lastIndexOf("ell", 4));//从后往前，从下标为4的位置开始找字符串"ell"第一次出现的位置
    }
    public static void main6(String[] args) {
        //字符串查找
        String s = "hello";
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            System.out.print(ch + " ");
        }
    }
    public static void main5(String[] args) {
        //字符串比较大小
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcdef");
        System.out.println(s1.compareTo(s2));//字符串中第二个字符不同，返回这两个字符的ASCII码值的差值
        System.out.println(s1.compareTo(s3));//字符串完全相同，ASCII码值的差值相同，输出0
        System.out.println(s1.compareTo(s4));//字符串中前3个字符相同，那么就输出这两个字符串长度的差值，不需要再比较ASCII码差值

        //大小写区别
        String s5 = new String("abC");
        System.out.println(s1.compareTo(s5));//输出结果会区分大小写，即返回大小写字母ASCII码值的差值
        //忽略大小写区别
        System.out.println(s1.compareToIgnoreCase(s5));//0
    }
    public static void main4(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 =s1;
        System.out.println(s1 == s2);//false - 尽管s1和s2实例化相同的内容，但还是false，因为它们引用的是不同的对象，地址不同
        System.out.println(s1 == s3);//true - s1和s3引用的是同一个对象，地址相同
        System.out.println(s2 == s3);//false
        System.out.println("=============");
        System.out.println(s1.equals(s2));//true - s1和s2引用的内容相同
        System.out.println(s1.equals(s3));//true
        System.out.println(s2.equals(s3));//true
    }
    public static void main3(String[] args) {
        //String对象的比较
        String s1 = new String("hello");
        String s2 = new String(" world");
        String s3 = s1;

        //==:引用类型比较的是引用中的地址是否相同（内置类型比较的是变量中的值，即内容是否相同）
        System.out.println(s1 == s2);//false
        System.out.println(s1 == s3);//true - s1和s3引用的是同一个对象，因此地址相同，是true
        System.out.println(s2 == s3);//false
        System.out.println("===================");
        //equals:引用类型比较的是引用的内容是否相同
        System.out.println(s1.equals(s2));//false
        System.out.println(s1.equals(s3));//true - s1和s3引用的内容相同，因此是true
        System.out.println(s2.equals(s3));//false
    }
    public static void main2(String[] args) {
        //s1和s2引用的是不同对象 s1和s3引用的是同一对象
        String s1 = new String("hello");
        String s2 = new String(" world");
        String s3 = s1;

        System.out.println(s1.length());//5

        int[] array = {1,2,3,4,5,6};
        System.out.println(array.length);//6

        System.out.println("hello".length());
    }
    public static void main1(String[] args) {
        //构造方法
        String str = "hello";//使用常量串构造

        String str2 = new String("word");//直接newString对象

        char[] value = {'a','b','c'};//使用字符数组进行构造
        String str3 = new String(value);

        //String str4 = new String();//使用 new String() 来创建一个空的字符串对象是完全没有必要的，因为你可以直接使用字面量 "" 来代替
        String str4 = "";

        System.out.println(str);
        System.out.println(str2);
        System.out.println(str3);
    }
}
